package com.itheima.xinxi.system.dao;

import com.itheima.xinxi.system.domain.Student;

import java.util.ArrayList;

/*
学生数据操作类
 */
public class OtherStudentDao implements BaseStudentDao {
    //创建数组
    //数据类型[] 数组名= new 数据类型[长度]
    private static ArrayList<Student>  stus = new ArrayList<>();

    static{
        Student stu1 = new Student("0001", "鹿晗", 29, "1991-01-01");
        Student stu2 = new Student("0002", "吴亦凡", 30, "1990-01-01");
        stus.add(stu1);
        stus.add(stu2);


    }
    //1050  1081
    //有没有参数 Student student
    ///有没有返回值 boolean
    public boolean addStudent(Student student) {
        stus.add(student);

        return true;
    }

    /**
     * 查询所有学生
     *
     * @return 学生数据
     */
    public Student[] findAllStudents() {

        Student[] students = new Student[stus.size()];

        for (int i = 0; i < stus.size(); i++) {
            //i students[i] stus.get(i)
            students[i]=stus.get(i);
        }
        return students;
    }

    /**
     * 根据id 删除学生信息
     * @param id 已经存在
     */
    public void deleteStudentById(String id) {
        //找到id 所在的索引
        int index = getIndex(id);
        //数组的指定索引赋值为null
       stus.remove(index);

    }


    //定义方法根据id 找索引
    public int getIndex(String id){
        //遍历数组
        for (int i = 0; i < stus.size(); i++) {
            //比较  找到直接返回
            //stus[i]
            Student stu = stus.get(i);
            if(stu!=null&&stu.getId().equals(id)){
                return i;
            }
        }
        //找不到返回 -1
        return -1;

    }

    public void updateStudent(String id, Student student) {

        //找到id 所在的索引
        int index = getIndex(id);
        //数组的指定索引赋值为新的学生对象student
        stus.set(index,student);


    }
}
